package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.searchPojo.Search;
import com.hmall.common.searchPojo.SearchVo;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.common.itemPojo.Item;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements com.hmall.item.service.ItemService {

    @Autowired
    private ItemMapper itemMapper;
    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageDTO<Item> selectPage(Integer page, Integer size) {
        Page<Item> itemPage = new Page<>(page,size);
        itemMapper.selectPage(itemPage, null);
        PageDTO<Item> itemPageDTO = new PageDTO<>(itemPage.getTotal(),itemPage.getRecords());
        return itemPageDTO;
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @Override
    public Item selectById(Long id) {
        return itemMapper.selectById(id);
    }

    /**
     * 新增商品
     * @param item
     */
    @Override
    public void insert(Item item) {
        item.setCreateTime(LocalDateTime.now());
        item.setUpdateTime(LocalDateTime.now());
        itemMapper.insert(item);
    }

    /**
     * 上下架
     * @param id
     * @param status
     */
    @Override
    public void upDuwn(Long id, Integer status) {
        itemMapper.upDuwn(id,status);
    }

    @Override
    public void updateById0(Item item) {
        item.setUpdateTime(LocalDateTime.now());
        itemMapper.updateById(item);
    }

    /**
     * 过滤项聚合功能
     * @param search
     * @return
     */
    @Override
    public SearchVo selectFilters(Search search) {
        SearchVo searchVo = new SearchVo();
        List<String> categorys = itemMapper.selectCategory();
        List<String> brands = itemMapper.selectBrand(search.getCategory());
        List<String> categorylist = categorys.stream().distinct().collect(Collectors.toList());
        List<String> brandlist = brands.stream().distinct().collect(Collectors.toList());
        searchVo.setCategory(categorylist);
        searchVo.setBrand(brandlist);
        return searchVo;
    }

    /**
     * 基本搜索功能
     * @param search
     * @return
     */
    @Override
    public PageDTO<Item> selectConditional(Search search) {
        Page<Item> itemPage = new Page<>(search.getPage(), search.getSize());
        QueryWrapper<Item> QueryWrapper = new QueryWrapper<>();
        QueryWrapper.eq(!StringUtils.isNullOrEmpty(search.getCategory()), "category",search.getCategory());
        QueryWrapper.eq(!StringUtils.isNullOrEmpty(search.getBrand()), "brand",search.getBrand());
        QueryWrapper.between(search.getMinPrice() != null || search.getMaxPrice() !=null,"price", search.getMinPrice(), search.getMaxPrice());
        QueryWrapper.orderByDesc("sold");
        QueryWrapper.gt("status", 0);
        itemMapper.selectPage(itemPage, QueryWrapper);
        return new PageDTO<>(itemPage.getTotal(), itemPage.getRecords());
    }
}
